//
//  GoodsSaveViewController.m
//  cityStar
//
//  Created by joyban on 15-1-15.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "GoodsSaveViewController.h"
#import "CommodityListCell.h"
#import "GetGoodsQuery.h"
#import "WaresDetailViewController.h"

NSString *const ComodityListCellID = @"SaveComodityListCellID";


@interface GoodsSaveViewController ()
@property (weak, nonatomic) IBOutlet UITableView *GoodsTable;
@property (weak, nonatomic) NSString *goodID;
@property (strong, nonatomic) NSArray *requestArray;
@property (assign, nonatomic) NSInteger page;

@end

@implementation GoodsSaveViewController

-(instancetype)initWithGoodID:(NSString *)goodID
{
    self = [super init];
    if (self) {
        self.goodID = goodID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的商品收藏";
    self.page = 0;
    [self addBackButton];
    [self setUpTableViews];
//    [self webRequest_getCollcetMessageWithGoodID:self.goodID];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self webRequest_getCollcetMessageWithGoodID:self.goodID];
}

#pragma mark - webRequest
-(void)webRequest_getCollcetMessageWithGoodID:(NSString *)goodID
{
    NSString *postUrl = [NSString stringWithFormat:@"%@/page/%@/type/%@",KInterface_GetCollection,[NSString stringWithFormat:@"%li",self.page],@"0"];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            self.requestArray = response[@"value"][@"content"];
            [self.GoodsTable reloadData];
        }else{
            self.requestArray = [[NSArray alloc]init];
            [self.GoodsTable reloadData];
//            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setUpTableViews
{
    [self.GoodsTable registerNib:[CommodityListCell nib] forCellReuseIdentifier:ComodityListCellID];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CommodityListCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.requestArray.count == 0)
    {
        return 1;
    }else{
        return self.requestArray.count;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    GetGoodsQuery *commodity = [GetGoodsQuery objectFromDictionary:self.requestArray[indexPath.row]];
    WaresDetailViewController *waresDetailViewController = [[WaresDetailViewController alloc] initWithCommodityID:commodity.goods];
    [self.navigationController pushViewController:waresDetailViewController animated:true];
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row == 0 && self.requestArray.count == 0)
    {
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"goodsSaveCellID"];
        if (cell==nil) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"goodsSaveCellID"];
        }
        cell.textLabel.text = @"暂无商品收藏";
        cell.textLabel.textColor = [UIColor colorWithWhite:0.580 alpha:1.000];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        return cell;
    }
    CommodityListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:ComodityListCellID];
    GetGoodsQuery *commodity = [GetGoodsQuery objectFromDictionary:self.requestArray[indexPath.row]];
    [ListdetailCell configWithCommidity:commodity];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return ListdetailCell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        GetGoodsQuery *commodity = [GetGoodsQuery objectFromDictionary:self.requestArray[indexPath.row]];
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/0",KInterface_RemoveCollection,commodity.uuid];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            DLog(@"%@",response);
            if (success) {
                [self webRequest_getCollcetMessageWithGoodID:self.goodID];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
